// 资源生命周期状态
export enum ResourceLifecycleStatus {
  DRAFT = 'DRAFT', // 草稿状态
  ACTIVE = 'ACTIVE', // 活跃状态
  SUSPENDED = 'SUSPENDED', // 暂停状态
  ARCHIVED = 'ARCHIVED', // 归档状态
}

// 资源可用性状态
export enum ResourceAvailabilityStatus {
  AVAILABLE = 'AVAILABLE', // 可用
  RESERVED = 'RESERVED', // 已预订
  IN_USE = 'IN_USE', // 使用中
  UNAVAILABLE = 'UNAVAILABLE', // 不可用
  FAILED = 'FAILED', // 故障
}

// 资源版本状态
export enum ResourceVersionStatus {
  DRAFT = 'DRAFT', // 草稿
  STAGED = 'STAGED', // 已暂存
  CANARY = 'CANARY', // 金丝雀发布
  ACTIVE = 'ACTIVE', // 活跃版本
  DEPRECATED = 'DEPRECATED', // 已废弃
  ROLLED_BACK = 'ROLLED_BACK', // 已回滚
}

// 行为执行状态
export enum BehaviorExecutionStatus {
  PENDING = 'PENDING', // 等待执行
  RUNNING = 'RUNNING', // 执行中
  SUCCESS = 'SUCCESS', // 执行成功
  FAILED = 'FAILED', // 执行失败
  COMPENSATING = 'COMPENSATING', // 补偿中
  COMPENSATED = 'COMPENSATED', // 已补偿
}

// 发布状态
export enum PublishStatus {
  DRAFT = 'DRAFT', // 草稿
  STAGED = 'STAGED', // 已暂存
  CANARY = 'CANARY', // 金丝雀发布
  ACTIVE = 'ACTIVE', // 活跃发布
  DEPRECATED = 'DEPRECATED', // 已废弃
}

// 用户自定义状态定义
export interface CustomStatusDefinition {
  id: string;
  name: string;
  code: string;
  description?: string;
  color?: string;
  icon?: string;
  isInitial?: boolean; // 是否为初始状态
  isFinal?: boolean; // 是否为终态
  allowedTransitions?: string[]; // 允许转换到的状态
  metadata?: Record<string, any>; // 额外元数据
}

// 资源类型自定义状态配置
export interface ResourceTypeCustomStatus {
  resourceType: string;
  statusDefinitions: CustomStatusDefinition[];
  stateMachine?: {
    name: string;
    description?: string;
    version: string;
    events?: Array<{
      name: string;
      from: string[];
      to: string;
      conditions?: string[];
      actions?: string[];
    }>;
  };
}

// 事件类型
export enum EventType {
  // 资源相关事件
  RESOURCE_CREATED = 'RESOURCE_CREATED',
  RESOURCE_UPDATED = 'RESOURCE_UPDATED',
  RESOURCE_DELETED = 'RESOURCE_DELETED',

  // 状态变更事件
  STATUS_CHANGED = 'STATUS_CHANGED',
  LIFECYCLE_CHANGED = 'LIFECYCLE_CHANGED',
  AVAILABILITY_CHANGED = 'AVAILABILITY_CHANGED',
  VERSION_CHANGED = 'VERSION_CHANGED',

  // 行为相关事件
  BEHAVIOR_EXECUTED = 'BEHAVIOR_EXECUTED',
  BEHAVIOR_STARTED = 'BEHAVIOR_STARTED',
  BEHAVIOR_COMPLETED = 'BEHAVIOR_COMPLETED',
  BEHAVIOR_FAILED = 'BEHAVIOR_FAILED',

  // 补偿相关事件
  COMPENSATION_TRIGGERED = 'COMPENSATION_TRIGGERED',
  COMPENSATION_COMPLETED = 'COMPENSATION_COMPLETED',

  // 发布相关事件
  PUBLISHED = 'PUBLISHED',
  DEPRECATED = 'DEPRECATED',
  CANARY_DEPLOYED = 'CANARY_DEPLOYED',

  // 自定义状态相关事件
  CUSTOM_STATUS_CHANGED = 'CUSTOM_STATUS_CHANGED',
  STATE_MACHINE_TRIGGERED = 'STATE_MACHINE_TRIGGERED',
  TRANSITION_VALIDATED = 'TRANSITION_VALIDATED',
  TRANSITION_REJECTED = 'TRANSITION_REJECTED',
}

// 完整的资源状态接口
export interface ResourceStatus {
  lifecycle: ResourceLifecycleStatus;
  availability: ResourceAvailabilityStatus;
  version: ResourceVersionStatus;
  custom?: {
    statusCode: string;
    statusName: string;
    metadata?: Record<string, any>;
  };
  lastUpdated: Date;
  reason?: string;
  operator?: string;
}

// 发布状态接口
export interface PublishStatusInfo {
  status: PublishStatus;
  version: string;
  publishTime?: Date;
  deprecateTime?: Date;
  canaryPercentage?: number;
}

// 行为状态接口
export interface BehaviorStatus {
  behaviorId: string;
  status: BehaviorExecutionStatus;
  startTime: Date;
  endTime?: Date;
  errorMessage?: string;
  compensationTriggered?: boolean;
}

// 事件接口
export interface Event {
  id: string;
  sourceId: string;
  sourceType: 'RESOURCE' | 'BEHAVIOR' | 'PUBLISH';
  type: EventType;
  payload: {
    oldStatus?: any;
    newStatus?: any;
    reason?: string;
    operator?: string;
    metadata?: any;
  };
  timestamp: Date;
  correlationId?: string;
}

// 状态变更历史接口
export interface StatusChangeHistory {
  id: string;
  resourceId: string;
  statusType: 'LIFECYCLE' | 'AVAILABILITY' | 'VERSION' | 'CUSTOM';
  oldStatus: string;
  newStatus: string;
  reason?: string;
  operator?: string;
  timestamp: Date;
  metadata?: Record<string, any>;
}
